<?php

namespace App\Http\Controllers\Api\Customer;

use Illuminate\Http\Request;
use App\Jobs\OrderSubmit;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Carbon\Carbon;
use App\Repository\UpThree\MainOrderRepostition;
use App\Http\Controllers\Api\BaseController;

class HuNanTelecomController extends BaseController
{
  protected $log = null;
  public function __construct()
  {
    $this->log = new Logger('HuNanTelecomPush');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/HuNanTelecomPush.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  // 电信那边推送过来的数据 
  public function pushOrderStatus(Request $Request)
  {
    // 刘志恒 让取消发货等状态接受
    // return json_encode(['responseCode' => 0, 'result' => '接受成功']);
    $getData = file_get_contents('php://input');
    $this->log->info('input' . ($getData));
    // return json_encode(['code' => 0, 'success' => true]);
    // $getData = '{"webOrder":"20230224163217891947803163","orderStatus":"Q1002","otherStatus":"AC002","sendNo":"1085401830714","logisticsName":"EMS","cp1":"xxl@","logisticsStatus":"10","orderStatusDesc":"国政通校验不通过"}';
    // $getData = '{"webOrder":"5051546131431392855","logisticsName":"顺丰快递","sendNo":"SF1389068390340","cp1":"zb@dy@qiaoqiao01"}';
    // 数据处理  
    if (empty($getData)) {
      return json_encode(['code' => 103, 'success' => false]);
    }
    if (!empty($getData) && is_string($getData)) {
      $getData = json_decode($getData, true);
      // var_dump($getData, 444444444);
    }
    if (empty($getData)) {
      return json_encode(['code' => 104, 'result' => false]);
    }
    $statusDatas = [
      0 => '提交中',
      1 => '提交上游',
      // 2 => '拒收单',
      10 => '审核中',
      11 => '待发货',
      20 => '已发货',
      30 => '已签收',
      40 => '已激活',
      50 => '异常单',
      51 => '取消单',
      60 => '失败单'
    ];
    // 100：初始状态,200：待支付,201：支付中,202：支付成功,203：支付失败,300：待订购,301：订购中,302：订购成功,303：订购失败,304：订购待重试,1000：待商户确认,1005：预处理成功,1010：预处理失败,1015：备货中,1020：已发货,1025：订购完成,1030：订购失败,1035：已激活,1040：激活失败，20：异步收单下单失败，其中302、303、1030、1035、20是订购终态
    $returnStatusArr = array(
      'pending' => 1,
      'processing' => 10,
      'completed' => 20,
      'Order_cancel' => 51,
      'order_cancel' => 51,
      'fail' => 60,
      'active_success' => 40
    );
    if (!empty($getData['otherStatus']) && $getData['otherStatus'] == 'AC002') {
      $getData['orderStatus'] = 'active_success';
    }
    // 将数据格式 修改成固定的数据格式
    $editData = [
      'status' => '',
      // 'express_company' => !empty($getData['sendNo']) ? $getData['sendNo'] : '',
      'express_company' => !empty($getData['logisticsName']) ? $getData['logisticsName'] : '',
      'express_number' => !empty($getData['sendNo']) ? $getData['sendNo'] : '',
      'three_msg' => !empty($getData['orderStatusDesc']) ? $getData['orderStatusDesc'] : '',
      'iccid' => !empty($getData['mobile']) ? $getData['mobile'] : '',
      'activat_at' => !empty($getData['active_time']) ? $getData['active_time'] : '',
    ];
    if (!empty($getData['orderStatus'])) {
      $editData['status'] = !empty($returnStatusArr[$getData['orderStatus']]) ? $returnStatusArr[$getData['orderStatus']] : '';
    }
    if (!empty($getData['logisticsName']) && !empty($getData['sendNo'])) {
      $editData['status'] = 20;
    }
    if (empty($editData['status'])) {
      return json_encode(['responseCode' => 1, 'result' => '未查询到状态']);;
    }
    $where = ['ordersno' => 'HK_' . $getData['webOrder']];
    $model = new MainOrderRepostition();
    $res = $model->followOrderStatus($where, $editData);
    // var_dump($res);
    $this->log->info('通知参数' . json_encode($getData) . '返回参数' . json_encode($res));
    if (!empty($res['code']) && $res['code'] == 200) {
      return json_encode(['responseCode' => 0, 'result' => '接受成功']);
      return 'success';
    }
    return json_encode(['responseCode' => 105, 'result' => '操作失败']);
    return 'error';
  }
}
